package com.foreknow.controller;

import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;
import com.foreknow.util.Pagination;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;

public class ManagerServlet extends HttpServlet {
    private int pageSize = 10;
    @Override
    public void init(ServletConfig config) throws ServletException{
        //要获取到web.xml中配置的全局初始化参数
        this.pageSize = Integer.parseInt(config.getServletContext().getInitParameter("pageSize"));
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String method = req.getParameter("q");
        if ("list".equals(method)) {
            try {
                list(req, resp);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } else if ("delete".equals(method)) {
            delete(req, resp);
        }
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    public void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException, SQLException {
        //查询所有留言信息
        //设置服务器端相应给客户端的内容的类型
        resp.setContentType("text/html;charset=utf-8");
        //客户端向服务器端发送请求的编码
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        //调用Service层的方法查询留言数据
        GuestbookService service = new GuestbookServiceImpl();
        //要从页面中获取到页码
        int pageNumber = 0;
        try {
            pageNumber = Integer.parseInt(req.getParameter("pageNo"));
        }catch (Exception e){
            pageNumber = 1;
        }


        Pagination pagination = service.getByPage(pageSize,pageNumber);
        //获取区间数据
        MappingFactory mappingFactory = MappingFactory.getInstance();
        EntityMapping mapping = mappingFactory.getMap(MappingFactory.GUESTBOOK_MAPPING);

        List<Object> list = pagination.getList(mapping);
        //将查询到的list集合保存到服务器端
        //将总记录数保存到request中
        req.setAttribute("total",pagination.getMaxElements());
//        out.println(list.size());
        //我们将list保存到request作用域中，然后在list.jsp中就能获取到数据了
        //在服务器端保存数据req.setAttribute(String,Object);
        req.setAttribute("list",list);
        //就可以将这个请求转发到list.jap
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.setContentType("text/html;charset=UTF-8");
        //客户端向服务器端发送请求的编码
        req.setCharacterEncoding("UTF-8");
        PrintWriter out = resp.getWriter();
        //获取到前台的参数id
        String id = req.getParameter("id");
        //
        GuestbookService service = new GuestbookServiceImpl();
        boolean isRight = service.delInfo(Integer.parseInt(id));
        if(isRight){
            //如果删除成功我们要转发资源，重新调用list方法查寻留言信息
            req.getRequestDispatcher("/user/manager?q=list").forward(req,resp);
        }else{
            //删除失败
            resp.sendRedirect("/foreknow_webdemo/user/manager?q=delete");

        }
    }
}


